package com.chen.palmar.project.set;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.ImageView;
import android.widget.TextView;

import com.chen.palmar.R;
import com.chen.palmar.base.BaseActivity;
import com.chen.palmar.common.constant.AppConstant;
import com.chen.palmar.common.datasource.DataCenter;
import com.chen.palmar.common.override.HttpSubscriber;
import com.chen.palmar.common.widget.view.AutoTabLayout;
import com.chen.palmar.common.widget.view.AutoToolbar;
import com.chen.palmar.entity.MessageCallEntity;
import com.chen.palmar.entity.MessageCommentEntity;
import com.chen.palmar.entity.MessageManager;
import com.chen.palmar.entity.MessageNoticeEntity;
import com.primb.androidlibs.ui.recyclerview.BaseQuickAdapter;
import com.primb.androidlibs.ui.recyclerview.BaseViewHolder;
import com.squareup.picasso.Picasso;

import butterknife.Bind;
import rx.Observable;
import rx.Subscription;
import rx.functions.Func3;

/**
 * @author Chen
 * @date 2018/1/9
 * 功能描述：
 */

public class MessageManagerActivity extends BaseActivity {
    @Bind(R.id.title_bar)
    TextView titleBar;
    @Bind(R.id.tool_bar)
    AutoToolbar toolBar;
    @Bind(R.id.tab_detail)
    AutoTabLayout tabDetail;
    @Bind(R.id.rv_message_list)
    RecyclerView rvMessageList;
    private MessageManager manager;
    private BaseQuickAdapter<MessageCallEntity.DataBean, BaseViewHolder> adapterCall;
    private BaseQuickAdapter<MessageCommentEntity.DataBean, BaseViewHolder> adapterComment;
    private BaseQuickAdapter<MessageNoticeEntity.DataBean, BaseViewHolder> adapterNotice;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        init();
    }

    @Override
    public void init() {
        toolBar.setNavigationOnClickListener(v -> MessageManagerActivity.this.finish());
        titleBar.setText("消息");

        tabDetail.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                setDataToView(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        rvMessageList.setLayoutManager(layoutManager);
        rvMessageList.setItemAnimator(new DefaultItemAnimator());

        adapterCall = new BaseQuickAdapter<MessageCallEntity.DataBean, BaseViewHolder>(R.layout.item_message) {
            @Override
            protected void convert(BaseViewHolder helper, MessageCallEntity.DataBean item) {
                helper.setText(R.id.tv_item_message_name, item.getName());
                helper.setText(R.id.tv_item_message_date, item.getCreate_at());
                helper.setText(R.id.tv_item_message_content, item.getContent());
                ImageView view = (ImageView) helper.getView(R.id.iv_item_message_head);
                Picasso.with(MessageManagerActivity.this).load(item.getAvatar()).resize(AppConstant.SCREEN_WIDTH / 2, AppConstant.SCREEN_WIDTH / 2).placeholder(R.mipmap.home_item_head_icon).error(R.mipmap.home_item_head_icon).into(view);

                ImageView store = (ImageView) helper.getView(R.id.iv_item_message_store);
                Picasso.with(MessageManagerActivity.this).load(item.getImg()).placeholder(R.mipmap.occupy_icon).error(R.mipmap.occupy_icon).into(store);
            }
        };
        adapterCall.setOnItemClickListener((adapter, view, position) -> {
            //            Intent intent = new Intent(MessageManagerActivity.this, ProducerDetailActivity.class);
            //            intent.putExtra("info", adapterCall.getData().get(position).getProductId() + "");
            //            startActivity(intent);
        });

        adapterComment = new BaseQuickAdapter<MessageCommentEntity.DataBean, BaseViewHolder>(R.layout.item_message) {
            @Override
            protected void convert(BaseViewHolder helper, MessageCommentEntity.DataBean item) {
                helper.setText(R.id.tv_item_message_name, item.getName());
                helper.setText(R.id.tv_item_message_date, item.getCreate_at());
                helper.setText(R.id.tv_item_message_content, item.getContent());
                ImageView view = (ImageView) helper.getView(R.id.iv_item_message_head);
                Picasso.with(MessageManagerActivity.this).load(item.getAvatar()).resize(AppConstant.SCREEN_WIDTH / 2, AppConstant.SCREEN_WIDTH / 2).placeholder(R.mipmap.home_item_head_icon).error(R.mipmap.home_item_head_icon).into(view);

                ImageView store = (ImageView) helper.getView(R.id.iv_item_message_store);
                Picasso.with(MessageManagerActivity.this).load(item.getImg()).placeholder(R.mipmap.occupy_icon).error(R.mipmap.occupy_icon).into(store);
            }
        };
        adapterComment.setOnItemClickListener((adapter1, view, position) -> {
            //            Intent intent = new Intent(MessageManagerActivity.this, ProducerDetailActivity.class);
            //            intent.putExtra("info", adapterComment.getData().get(position).getMesgId() + "");
            //            startActivity(intent);
        });

        adapterNotice = new BaseQuickAdapter<MessageNoticeEntity.DataBean, BaseViewHolder>(R.layout.item_message_notice) {
            @Override
            protected void convert(BaseViewHolder helper, MessageNoticeEntity.DataBean item) {
                helper.setText(R.id.tv_item_message_name, item.getTitle());
                helper.setText(R.id.tv_item_message_date, item.getCreate_at());
                helper.setText(R.id.tv_item_message_content, item.getContent());
            }
        };
        adapterNotice.setOnItemClickListener((adapter1, view, position) -> {
            //            Intent intent = new Intent(MessageManagerActivity.this, ProducerDetailActivity.class);
            //            intent.putExtra("info", adapterNotice.getData().get(position).getMesgId() + "");
            //            startActivity(intent);
        });


        try {
            getListData();
        } catch (Exception e) {
            e.printStackTrace();
            showToast("服务器数据异常");
        }

    }

    private void getListData() {
        Observable<MessageCallEntity> messageCallInfo = DataCenter.messageCallInfo();
        Observable<MessageCommentEntity> messageCommentInfo = DataCenter.messageCommentInfo();
        Observable<MessageNoticeEntity> messageNoticeInfo = DataCenter.messageNoticeInfo();

        Subscription subscribe = Observable.zip(messageCallInfo, messageCommentInfo, messageNoticeInfo, new Func3<MessageCallEntity, MessageCommentEntity, MessageNoticeEntity, MessageManager>() {
            @Override
            public MessageManager call(MessageCallEntity messageCallEntity, MessageCommentEntity messageCommentEntity, MessageNoticeEntity messageNoticeEntity) {
                MessageManager manager = new MessageManager();
                manager.setCallEntity(messageCallEntity);
                manager.setCommentEntity(messageCommentEntity);
                manager.setNoticeEntity(messageNoticeEntity);
                return manager;
            }
        }).subscribe(new HttpSubscriber<MessageManager>(this, showLoading()) {

            @Override
            public void onNext(MessageManager messageManager) {
                MessageManagerActivity.this.manager = messageManager;
                setDataToView(0);
            }
        });
        subscription.add(subscribe);
    }

    private void setDataToView(int position) {
        if (manager == null) {
            showToast("数据加载失败");
            return;
        }
        switch (position) {
            case 0:
                rvMessageList.setAdapter(adapterCall);
                adapterCall.setNewData(manager.getCallEntity().getData());
                break;
            case 1:
                rvMessageList.setAdapter(adapterComment);
                adapterComment.setNewData(manager.getCommentEntity().getData());
                break;
            case 2:
                rvMessageList.setAdapter(adapterNotice);
                adapterNotice.setNewData(manager.getNoticeEntity().getData());
                break;
        }
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_message_list;
    }
}
